import { reqGetShops, reqGetCityList } from '@/api/home/shops.js'
import { reqGetShopsMenu, reqGetShopsGoods, reqGetShopsConfig } from '@/api/home/menu.js'
import axios from 'axios'

export default {
  state: {
    shopsList: [],
    shopsMenu: {},
    shopsGoods: {},
    shopsConfig: {},
    shopsItem: {},
    initShoppingCart: [],
    shoppingCart: [],
    menuScroll: {},

    citysList: [],
    positionMode: false, // 手动或默认定位模式
    location: { // 默认定位
      // address: '',
      // location: undefined,
      // lnglat: undefined
    },
    changeLocation: { // 手动定位
      // address: '',
      // location: undefined,
      // lnglat: undefined
    }
  },
  mutations: {
    // 存储菜单页面滚动位置数据
    saveMenuScroll(state, data) {
      if (!data.cate && !data.goods) return (state.menuScroll.key = data.key)
      state.menuScroll = data
    },
    initShopsList(state, data) {
      state.shopsList = data
    },
    initMenuList(state, data) {
      state.shopsMenu = data
    },
    initGoodsList(state, data) {
      state.shopsGoods = data
    },
    initShopsConfig(state, data) {
      state.shopsConfig = data
    },
    selectShops(state, data) {
      state.shopsItem = data
    },
    // 加入、减少原始购物车数据
    changeSpCart(state, data) {
      var cart = state.initShoppingCart
      if (data.delete) {
        cart.some((o, i) => {
          if (o.id === data.id) {
            cart.splice(i, 1)
            return true
          }
        })
      } else {
        cart.push(data)
      }
      this.commit('afterSpCart', cart)
    },
    // 整理去重原始购物车数据
    afterSpCart(state, data) {
      var cart = []
      data.forEach(item => {
        var flag = cart.find(o => o.id === item.id && item.type !== 5)
        if (!flag) {
          item.amount = 1
          cart.push(item)
          return
        }
        cart.some(o => {
          if (item.id === o.id) {
            o.amount++
            return true
          }
        })
      })
      state.shoppingCart = cart
    },
    // 初始化定位数据
    initLocation(state, data) {
      state.location = data
    },
    // 初始化全国城市列表数据
    initCityList(state, data) {
      state.citysList = data.city
    },
    // 改变定位模式
    changePositionMode(state, data) {
      state.positionMode = data
    },
    // 手动更改定位
    changePosition(state, data) {
      state.changeLocation = data
    }
  },
  actions: {
    async getShopsList({ commit }, data) {
      const result = await reqGetShops()
      if (result.code === 200) {
        // console.log(result)
        commit('initShopsList', result.data)
      } else {
        return result.message
      }
    },
    async getMenuList({ commit }, data) {
      const result = await reqGetShopsMenu(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initMenuList', result.data)
      } else {
        return result.message
      }
    },
    async getGoodsList({ commit }, data) {
      const result = await reqGetShopsGoods(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initGoodsList', result.data.item)
      } else {
        return result.message
      }
    },
    async getShopsConfig({ commit }, data) {
      const result = await reqGetShopsConfig(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initShopsConfig', result.data)
      } else {
        return result.message
      }
    },
    // 获取全国城市列表
    async getCityList({ commit }) {
      const result = await reqGetCityList()
      if (result.code === 200) {
        // console.log(result)
        commit('initCityList', result.data)
      } else {
        return result.message
      }
    },
    // 获取定位坐标编码
    getLocation(content, data) {
      AMap.plugin('AMap.Geolocation', function () {
        var geolocation = new AMap.Geolocation({
          // 是否使用高精度定位，默认：true
          enableHighAccuracy: true,
          // 设置定位超时时间，默认：无穷大
          timeout: 10000
        })
        geolocation.getCurrentPosition(function (status, result) {
          if (status === 'complete') {
            // console.log(result)
            const lng = result.position.lng.toString()
            const lat = result.position.lat.toString()
            content.dispatch('getAddress', (lng + ',' + lat).toString())
          } else {
            // console.log(result)
            content.commit('initLocation', { address: '定位失败,请刷新重试' })
          }
        })
      })
    },
    // 根据坐标返回地址(逆地理编码，获取详细地址)
    getAddress({ commit }, data) {
      axios({
        method: 'GET',
        url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
        params: {
          key: '8857783cc0db9ee595f99da7893d2e56',
          location: data
        }
      }).then(result => {
        if (result.status === 200) {
          if (result.data.status === '0') return commit('initLocation', { address: '定位失败,请刷新重试' })
          var address = result.data.regeocode.formatted_address
          var location = result.data.regeocode.addressComponent
          commit('initLocation', { address: address, location: location, lnglat: data })
        } else {
          commit('initLocation', { address: '定位失败,请刷新重试' })
        }
      })
    }
  },
  getters: {
  }
}
